用okhtt/retrofit下载文件
项目中需要在开屏页下载东西,在github上发现一个好用的框架
大家可以去https://github.com/lingochamp/FileDownloader/blob/master/README-zh.md查看
关于retrofit的用法就不多说了,这个框架依赖于okhttp 3.4.1
- 首先在项目中引用
compile 'com.liulishuo.filedownloader:library:0.3.4' - 然后在Application的onCreate()中初始化
FileDownloader.init(applicationContext); - 在工具类中丢进这2个方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117/**
* 单任务下载
*/
public static void downloadFile(String path, String url) {
FileDownloader.getImpl().create(url)
.setPath(path)
.setListener(new FileDownloadListener() {
protected void pending(BaseDownloadTask task, int soFarBytes, int totalBytes) {
}
protected void connected(BaseDownloadTask task, String etag, boolean isContinue, int soFarBytes, int totalBytes) {
}
protected void progress(BaseDownloadTask task, int soFarBytes, int totalBytes) {
}
protected void blockComplete(BaseDownloadTask task) {
}
protected void retry(final BaseDownloadTask task, final Throwable ex, final int retryingTimes, final int soFarBytes) {
}
protected void completed(BaseDownloadTask task) {
}
protected void paused(BaseDownloadTask task, int soFarBytes, int totalBytes) {
}
protected void error(BaseDownloadTask task, Throwable e) {
}
protected void warn(BaseDownloadTask task) {
}
}).start();
}
/**
* 多任务下载
* 参数1:url的集合 参数2:下载的路径
*/
public static void downloadFiles(List<RingListJson.RingBean> urls,File dir) {
final FileDownloadListener downloadListener = new FileDownloadListener() {
protected void pending(BaseDownloadTask task, int soFarBytes, int totalBytes) {
}
protected void connected(BaseDownloadTask task, String etag, boolean isContinue, int soFarBytes, int totalBytes) {
}
protected void progress(BaseDownloadTask task, int soFarBytes, int totalBytes) {
}
protected void blockComplete(BaseDownloadTask task) {
}
protected void retry(final BaseDownloadTask task, final Throwable ex, final int retryingTimes, final int soFarBytes) {
}
protected void completed(BaseDownloadTask task) {
}
protected void paused(BaseDownloadTask task, int soFarBytes, int totalBytes) {
}
protected void error(BaseDownloadTask task, Throwable e) {
}
protected void warn(BaseDownloadTask task) {
}
};
final FileDownloadQueueSet queueSet = new FileDownloadQueueSet(downloadListener);
final List<BaseDownloadTask> tasks = new ArrayList<>();
for (int i = 0; i < urls.size(); i++) {
if (FileDownloader.getImpl().create((urls.get(i).url)).isReusedOldFile()) {
continue;
}
tasks.add(FileDownloader.getImpl()
.create(urls.get(i).url)
.setTag(i + 1)
.setPath(dir.getAbsolutePath()+ File.separator+urls.get(i).name.concat(".aac")));
}
/*由于是队列任务, 这里是我们假设了现在不需要每个任务都回调`FileDownloadListener#progress`,
我们只关系每个任务是否完成,
所以这里这样设置可以很有效的减少ipc.*/
queueSet.disableCallbackProgressTimes();
// 所有任务在下载失败的时候都自动重试一次
queueSet.setAutoRetryTimes(1);
queueSet.downloadTogether(tasks);
queueSet.start();
} - 在需要下载的地方调用者2个方法即可
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22Retrofit retrofit = new Retrofit.Builder().baseUrl("http:/xxx.xxx.com").addConverterFactory(GsonConverterFactory.create()).build();
RingListResult ringListResult = retrofit.create(RingListResult.class);
Call<RingListJson> call = ringListResult.getRings();
final File dir = D8Application.applicationContext.getFilesDir();
final File rings = new File(dir + "/rings");
if (!rings.exists()) {
rings.mkdirs();
}
call.enqueue(new Callback<RingListJson>() {
public void onResponse(Call<RingListJson> call, Response<RingListJson> response) {
RingListJson ringListJson = response.body();
List<RingListJson.RingBean> ringList = ringListJson.ring; //在服务器拿到url的集合
Api.downloadFiles(ringList,rings); //传进方法里 Api是自己的工具类
}
public void onFailure(Call<RingListJson> call, Throwable t) {
}
});
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 七夜的小屋!





